/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClientRegister.java
 *
 * Created on Nov 6, 2012, 3:50:51 PM
 */

package BankClientPrototype;

import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author sjt5097
 */
public class ClientRegister extends javax.swing.JFrame {

    ClientHandler ch = new ClientHandler();
    /** Creates new form ClientRegister */
    public ClientRegister() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textfieldFirstName = new javax.swing.JTextField();
        labelFirstName = new javax.swing.JLabel();
        labelLastName = new javax.swing.JLabel();
        textfieldLastName = new javax.swing.JTextField();
        labelSavings = new javax.swing.JLabel();
        textfieldSavings = new javax.swing.JTextField();
        labelChecking = new javax.swing.JLabel();
        textfieldChecking = new javax.swing.JTextField();
        buttonSubmit = new javax.swing.JButton();
        labelLastName1 = new javax.swing.JLabel();
        textfieldUsername = new javax.swing.JTextField();
        labelLastName2 = new javax.swing.JLabel();
        textfieldPassword = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registration and Authorization");

        labelFirstName.setText("First Name:");

        labelLastName.setText("Last Name:");

        labelSavings.setText("Savings Account Number");

        labelChecking.setText("Checking Account Number");

        buttonSubmit.setText("Authorize and Register");
        buttonSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSubmitActionPerformed(evt);
            }
        });

        labelLastName1.setText("Username:");

        labelLastName2.setText("Password:");

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BankClientPrototype/asterisk.jpg"))); // NOI18N

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BankClientPrototype/asterisk.jpg"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BankClientPrototype/asterisk.jpg"))); // NOI18N

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/BankClientPrototype/asterisk.jpg"))); // NOI18N

        jLabel1.setText("Field marked with asterisks(*) are required.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(buttonSubmit)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelChecking)
                                    .addComponent(labelSavings)
                                    .addComponent(labelFirstName)
                                    .addComponent(labelLastName)
                                    .addComponent(labelLastName1)
                                    .addComponent(labelLastName2))
                                .addGap(34, 34, 34)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(textfieldFirstName)
                                    .addComponent(textfieldLastName)
                                    .addComponent(textfieldUsername)
                                    .addComponent(textfieldSavings)
                                    .addComponent(textfieldChecking)
                                    .addComponent(textfieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelFirstName)
                    .addComponent(textfieldFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLastName)
                    .addComponent(textfieldLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelLastName1)
                        .addComponent(textfieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelLastName2)
                        .addComponent(textfieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(textfieldSavings, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(labelSavings))
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelChecking)
                        .addComponent(textfieldChecking, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel6))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSubmit)
                    .addComponent(jButton1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void buttonSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSubmitActionPerformed
        //Lookup user last name
        //Lookup user first name
        //Check account numbers
        //If info is valid, insert username and password info
        //Display confirmation or rejection dialog
        //If confirmed, go back to login screen
        //If rejected, go back to register screen

        boolean accepted;
        String checking = textfieldChecking.getText();
        String savings = textfieldSavings.getText();
        String username = textfieldUsername.getText();
        int length = textfieldPassword.getPassword().length;
        
        if (username.isEmpty() || (length == 0)
                || savings.isEmpty() || checking.isEmpty()) {
            textfieldUsername.setBackground(Color.WHITE);
            textfieldPassword.setBackground(Color.WHITE);
            textfieldSavings.setBackground(Color.WHITE);
            textfieldChecking.setBackground(Color.WHITE);
            JOptionPane.showMessageDialog(null, "Please enter the required information.", "Invalid Input", 1);
            
            if(username.isEmpty()){
            textfieldUsername.setBackground(Color.PINK);}
            
            if(length == 0){
            textfieldPassword.setBackground(Color.PINK);}
            
            if(savings.isEmpty()){
            textfieldSavings.setBackground(Color.PINK);}
            
            if(checking.isEmpty()){
            textfieldChecking.setBackground(Color.PINK);}
            
            textfieldFirstName.requestFocusInWindow();
        } else {
            textfieldUsername.setBackground(Color.WHITE);
            textfieldPassword.setBackground(Color.WHITE);
            textfieldSavings.setBackground(Color.WHITE);
            textfieldChecking.setBackground(Color.WHITE);
            
            if((savings.equals("0")) && (checking.equals("0")))
            {
                JOptionPane.showMessageDialog(null, "Please enter one of your account numbers.");
                textfieldSavings.requestFocusInWindow();
            }
            if((!savings.equals("0")) && (!checking.equals("0")))
            {
                int userInput = JOptionPane.showConfirmDialog(null, "Please enter only one of your accounts. "
                        + "\nSelect Yes to keep your Savings account, "
                        + "\nselect No to keep your Checking account, and "
                        + "\ncancel to return to the registration window.", "Please select your option", 1);
                if(userInput == 0) // wants to keep savings
                {
                    textfieldChecking.setText("0");
                    accepted = ch.registering(textfieldUsername.getText(),
                                    textfieldPassword.getPassword(),
                                    Integer.parseInt(textfieldSavings.getText()),
                                    Integer.parseInt(textfieldChecking.getText()));
            
                    if (accepted) {
                        JOptionPane.showMessageDialog(null, "Registered !");
                        ch.fromRegister(this);
                    } else if (!accepted) {
                        JOptionPane.showMessageDialog(null, "Sorry, we cannot seem to authenticate your identity");
                    }
                }
                else if(userInput == 1) // wants to keep checking
                {
                    textfieldSavings.setText("0");
                    accepted = ch.registering(textfieldUsername.getText(),
                                    textfieldPassword.getPassword(),
                                    Integer.parseInt(textfieldSavings.getText()),
                                    Integer.parseInt(textfieldChecking.getText()));
            
                    if (accepted) {
                        JOptionPane.showMessageDialog(null, "Registered !");
                        ch.fromRegister(this);
                    } else if (!accepted) {
                        JOptionPane.showMessageDialog(null, "Sorry, we cannot seem to authenticate your identity");
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Enter only one of your account numbers.");
                    textfieldSavings.setText("");
                    textfieldChecking.setText("");
                }
            }
            if((savings.equals("0") && (!checking.equals("0"))) || (!savings.equals("0") && (checking.equals("0"))))
            {
                accepted = ch.registering(textfieldUsername.getText(),
                                    textfieldPassword.getPassword(),
                                    Integer.parseInt(textfieldSavings.getText()),
                                    Integer.parseInt(textfieldChecking.getText()));
            
                if (accepted) {
                    JOptionPane.showMessageDialog(null, "Registered !");
                    ch.fromRegister(this);
                } else if (!accepted) {
                    JOptionPane.showMessageDialog(null, "Sorry, we cannot seem to authenticate your identity");
                }
            }
        }
}//GEN-LAST:event_buttonSubmitActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ch.fromRegister(this);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ClientRegister().setVisible(true);
            }
        });

    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonSubmit;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel labelChecking;
    private javax.swing.JLabel labelFirstName;
    private javax.swing.JLabel labelLastName;
    private javax.swing.JLabel labelLastName1;
    private javax.swing.JLabel labelLastName2;
    private javax.swing.JLabel labelSavings;
    private javax.swing.JTextField textfieldChecking;
    private javax.swing.JTextField textfieldFirstName;
    private javax.swing.JTextField textfieldLastName;
    private javax.swing.JPasswordField textfieldPassword;
    private javax.swing.JTextField textfieldSavings;
    private javax.swing.JTextField textfieldUsername;
    // End of variables declaration//GEN-END:variables

}
